using System;
using System.Timers;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Notifications;
using Avalonia.Interactivity;
using Avalonia.Threading;
using SukiUI.Controls;
using SukiUI.Dialogs;
using SukiUI.Toasts;

namespace Launcher;

public partial class MainWindow : SukiWindow
{
    private const float ToastDismissTime = 3;
    private readonly Timer _changedRecordTimer = new(TimeSpan.FromSeconds(1)) { AutoReset = false };
    private static ISukiToastManager _toast = new SukiToastManager();
    private static ISukiDialogManager _dialog = new SukiDialogManager();
    public MainWindow()
    {
        InitializeComponent();
        ToastHost.Manager = _toast;
        DialogHost.Manager = _dialog;
        DataContext = new MainModel(this);

        if (Setting.Instance.WindowRect != null)
        {
            var rect = (PixelRect)Setting.Instance.WindowRect.Value;
            Width = rect.Width;
            Height = rect.Height;
            Position = rect.Position;
        }
        else
        {
            SetWindowHalfCenter();
        }

        _changedRecordTimer.Elapsed += RecordWindowRect;
    }

    public static void ShowToast(NotificationType type,string title,string content)
    {
        Dispatcher.UIThread.InvokeAsync(() =>
        {
            _toast.CreateToast()
                .Dismiss().After(TimeSpan.FromSeconds(ToastDismissTime))
                .Dismiss().ByClicking()
                .WithTitle(title)
                .WithContent(content)
                .OfType(type)
                .Queue();
        });
    }

    public static void ShowDialog(string tips, Action confirmAction)
    {
        var dialog = _dialog.CreateDialog()
            .WithTitle("提示")
            .WithContent(tips)
            .WithActionButton("取消", _ => { },true)
            .WithActionButton("确定", _=>confirmAction.Invoke(),true);
        dialog.SetCanDismissWithBackgroundClick(true);
        dialog.TryShow();
    }
    
    private void Window_OnLoaded(object? sender, RoutedEventArgs e)
    {
        SizeChanged += Window_OnSizeChanged;
        PositionChanged += Window_OnPositionChanged;
    }

    private void Window_OnSizeChanged(object? sender, SizeChangedEventArgs e)
    {
        if (e.PreviousSize == new Size(0, 0))
        {
            return;
        }
        
        _changedRecordTimer.Stop();
        _changedRecordTimer.Start();
    }

    private void Window_OnPositionChanged(object? sender, PixelPointEventArgs e)
    {
        _changedRecordTimer.Stop();
        _changedRecordTimer.Start();
    }

    private void RecordWindowRect(object? sender, ElapsedEventArgs e)
    {
        Dispatcher.UIThread.InvokeAsync(() =>
        {
            Setting.Instance.WindowRect = new PixelRect(Position, new PixelSize((int)Width, (int)Height));
            Setting.Save();
        });
    }

    private void SetWindowHalfCenter()
    {
        // 获取当前窗口所在的屏幕信息。
        var screenFromVisual = Screens.ScreenFromVisual(this);

        // 获取屏幕的边界矩形。
        var screenRect = screenFromVisual.Bounds;
        // 获取屏幕的缩放比例。
        var screenScaling = screenFromVisual.Scaling;

        // 获取屏幕的宽度和高度。
        var screenWidth = screenRect.Width;
        var screenHeight = screenRect.Height;

        Width = screenWidth / 2f / screenScaling;
        Height = screenHeight / 2f / screenScaling;

        // 获取窗口的宽度和高度。
        var windowWidth = Width;
        var windowHeight = Height;

        // 计算窗口的位置，使其居中显示在屏幕上。
        var x = (screenWidth - windowWidth * screenScaling) / 2;
        var y = (screenHeight - windowHeight * screenScaling) / 2;

        // 设置窗口的位置。
        Position = new PixelPoint((int)x, (int)y);
    }
}